package com.corner.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.app.GlobalParams;
import com.corner.presenter.base.BasePresenter;
import com.corner.ui.base.BaseActivity;
import com.corner.utils.view.AudioHelper;
import com.corner.utils.view.BaseRecordNoticeCallback;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;


public class Record4Pic extends BaseActivity implements OnClickListener {
    public static final String AUDIO_PATH = "AudioPath";
    public static final String PICTUREPATH = "picturePath";
    public static final int RESULT_OK = 1;
    public static final int RESULT_CANCEL = 2;
    public static final int RESULT_RESELECT = 3;
    @FindView(R.id.iv_record_for_pic_show_pic)
    private ImageView ivPicShow;
    @FindView(R.id.ib_record_for_pic_record)
    private ImageButton ibRecord;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_record_for_pic_go_back)
    private TextView tvBack;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_record_for_pic_reselect_pic)
    private TextView tvReselect;

    @Override
    protected BasePresenter createP() {
        return null;
    }

    @Override
    protected void setOnClick() {
        tvBack.setOnClickListener(this);
        tvReselect.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        ivPicShow.setImageDrawable(Drawable.createFromPath(getIntent()
                .getStringExtra(PICTUREPATH)));
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_record_for_pic);
    }

    @Override
    public void initView() {
        super.initView();
        AudioHelper audioHelper = new AudioHelper(ibRecord,
                new AudioHelper.OnFinishRecordCallback() {

                    @Override
                    public void finish(boolean isOk, String path) {
                        if (isOk) {
                            Intent data = getIntent();
                            data.putExtra(AUDIO_PATH, path);
                            setResult(RESULT_OK, data);
                            Record4Pic.this.finish();
                        }
                    }
                });
        audioHelper.setRecoderNoticeCallback(new BaseRecordNoticeCallback(
                ivPicShow, getApplicationContext()) {
            @Override
            protected void show(PopupWindow popWindow, View parent) {
                popWindow.showAtLocation(parent, Gravity.CENTER_HORIZONTAL
                        | Gravity.TOP, 0, GlobalParams.screenHeight / 4);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_record_for_pic_go_back:
                setResult(RESULT_CANCEL, getIntent());
                break;
            case R.id.tv_record_for_pic_reselect_pic:
                setResult(RESULT_RESELECT, getIntent());
                break;
        }
        finish();
    }
}
